import java.io.*;

/**
 * @author jcl_hqu
 * @date 2021/3/24 19:26
 */
    /**
     * 示例：执行进程并返回结果
     */
    public   class  Test {

        public static final int SUCCESS = 0;             //  表示程序执行成功

        public static final String COMMAND = "D:\\hqu_2020\\OperateSystem\\EX1\\src\\Producer.java";     //  要执行的语句

        public static final String SUCCESS_MESSAGE = " 程序执行成功！ ";

        public static final String ERROR_MESSAGE = " 程序执行出错： ";

        public static void main(String[] args) throws Exception {

            //  执行程序
            Process process = Runtime.getRuntime().exec(COMMAND);

            //  打印程序输出
            readProcessOutput(process);

            //  等待程序执行结束并输出状态
            int exitCode = process.waitFor();

            if (exitCode == SUCCESS) {
                System.out.println(SUCCESS_MESSAGE);
            } else {
                System.err.println(ERROR_MESSAGE + exitCode);
            }
        }

        /**
         * 打印进程输出
         *
         * @param process 进程
         */
        private static void readProcessOutput(final Process process) throws IOException {
            //  将进程的正常输出在 System.out 中打印，进程的错误输出在 System.err 中打印
            read(process.getInputStream(), System.out);
            read(process.getErrorStream(), System.err);
        }

        //  读取输入流
        private static void read(InputStream inputStream, PrintStream out) throws IOException {
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    out.println(line);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {

                inputStream.close();
            }
        }
    }
